from pytest_yaml_yoyo import my_builtins
from pytest_yaml_yoyo.http_session import HttpSession
import hashlib
import datetime
import time
import pytest


#  登录万店掌端，设置wdz_token，更新接口header
@pytest.fixture(scope="session", autouse=True)
def login_WDZ(requests_session: HttpSession, environ):
    """登录密码使用MD5加密"""
    body = {
        "userName": environ.USERNAME_WDZ,
        "password": hashlib.md5(environ.PASSWORD_WDZ.encode(encoding='UTF-8')).hexdigest(),
        "loginType": 1
    }
    r = requests_session.send_request(method="post", url="api/ovopark-privilege/user/login", json=body)
    token = r.json()['data']['token']
    h = {
        "Ovo-Authorization": token
    }
    requests_session.headers.update(h)
    yield requests_session


#  登录小鸟探店app，设置dt_token
@pytest.fixture(scope="session", autouse=True)
def dt_token(requests_session: HttpSession, environ):
    body = {
        "userName": environ.USERNAME_APP,
        "password": environ.PASSWORD_APP,
        "appKey": "9aa77d20c0883a98",
        "loginType": 3
    }
    r = requests_session.send_request(method="post", url="shopweb-detect-customer/users/login", data=body)
    dt_token = r.json()['data']['token']
    print(f"店探token: {dt_token}")
    return dt_token


@pytest.fixture(scope='session', autouse=True)
def sync_timer():
    current_time = time.time()
    next_second = int(current_time) + 1
    sleep_time = next_second - current_time
    time.sleep(sleep_time)


# 获取当前时间 注册到my_builtins yaml文件直接调用
def endTime(days):
    time = (datetime.datetime.now() + datetime.timedelta(days=days)).strftime('%Y-%m-%d')
    return time


my_builtins.endTime = endTime


# 获取当前时间 注册到my_builtins yaml文件直接调用
def deadline(days):
    deadTime = (datetime.datetime.now() + datetime.timedelta(days=days)).strftime('%Y-%m-%d %H:%M:%S')
    return deadTime


my_builtins.deadline = deadline
if __name__ == "__main__":
    deadline(3)
